package cn.nuist.controller;

import cn.nuist.entity.Products;
import cn.nuist.entity.Supplier;
import cn.nuist.service.ProductService;
import cn.nuist.service.SupplierService;
import cn.nuist.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    private ProductService productService;
    private SupplierService supplierService;

    @Autowired
    public ProductController(ProductService productService ,SupplierService supplierService) {
        this.productService = productService;
        this.supplierService = supplierService;
    }

    @RequestMapping("getAll")
    public String getAll(@RequestParam("target")String targetPageNum,Model model){
        if (targetPageNum == null || targetPageNum.trim().length() == 0) {
            targetPageNum = "1";
        }
        Integer target = null;
        try {
            target = Integer.parseInt(targetPageNum);
        } catch (NumberFormatException e) {
            target = 1;
        }
        Page<Products> products = productService.getViewPage(target);
        //System.out.println("***"+products);
        model.addAttribute("products",products);
        return "product/productList";
    }

    @RequestMapping("showdetail")
    public String showdetail(@RequestParam("productName")String productName,Model model){
        Products productByName = productService.getProductByName(productName);
        model.addAttribute("product",productByName);
        return "product/productView";
    }

    @RequestMapping("addView")
    public String addView(Model model){
        List<Supplier> allSupplier = supplierService.getAllSupplier();
        model.addAttribute("suppliers",allSupplier);
        return "product/productAdd";
    }

    @RequestMapping("add")
    public String add(Products product){
        //System.out.println(product);
        productService.add(product);
        return "redirect:/product/showdetail?productName="+product.getProductName();
    }

    @RequestMapping("delete")
    public String delete(@RequestParam("productID")int productID){
        productService.delete(productID);
        return "redirect:/product/getAll?target=1";
    }

    @RequestMapping("update")
    public String update(Products product){
        productService.update(product);
        //System.out.println("****"+product);
        return "redirect:/product/showdetail?productName="+product.getProductName();
    }

    @RequestMapping("updateView")
    public String updateView(@RequestParam("productID")int productID,Model model){
        Products productByID = productService.getProductByID(productID);
        model.addAttribute("product",productByID);
        List<Supplier> allSupplier = supplierService.getAllSupplier();
        model.addAttribute("supplier",allSupplier);
        return "/product/productUpdate";
    }

    @RequestMapping("search")
    public String search(String productName) throws UnsupportedEncodingException {
        Products productLike = productService.getProductLike(productName);
        return "redirect:/product/showdetail?productName="+ URLEncoder.encode(productLike.getProductName(),"UTF-8");
    }

}
